<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
            v-model="queryParams.taskName"
            placeholder="请输入任务名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抄送人" prop="sendUserName">
        <el-input
            v-model="queryParams.sendUserName"
            placeholder="请输入抄送人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
              v-for="dict in copy_message_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:copy:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="copyList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="65" align="center" />
      <el-table-column label="任务编号" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="taskName" :show-overflow-tooltip="true" />
      <el-table-column label="流程名称" align="center" prop="procName" :show-overflow-tooltip="true" />
      <el-table-column label="抄送人" align="center" prop="sendUserName" />
      <el-table-column label="抄送时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="copy_message_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link size="small" type="primary" icon="Tickets" @click="handleFlowDetail(scope.row)" >详情</el-button>
          <el-button link size="small" type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:copy:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!--流程详情-->
    <el-drawer
        v-model="open"
        title="流程详情"
        size="60%"
    >
      <process-detail v-if="open" :flowTaskData="flowTaskData"/>
    </el-drawer>

  </div>
</template>

<script setup name="Copy">
import { listCopy, delCopy } from "@/api/flowable/copy";
import ProcessDetail from "@/views/flowable/detail";
import router from "@/router";
const { proxy } = getCurrentInstance();
const { copy_message_status } = proxy.useDict('copy_message_status');

const copyList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskName: null,
    sendUserName: null,
    status: null,
  },
  flowTaskData: {}
});

const { queryParams,flowTaskData } = toRefs(data);

/** 查询抄送信息列表 */
function getList() {
  loading.value = true;
  listCopy(queryParams.value).then(response => {
    copyList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 流程流转记录 */
function handleFlowDetail(row){
  flowTaskData.value = {
    procInsId: row.procInsId,
    deployId: row.deployId,
    executionId: row.executionId,
    taskId: row.taskId,
    id: row.id,
    status: row.status,
    procDefName: row.procName,
    finishTime: 'finished',
    revokeTask: false,
  };
  open.value = true;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除抄送信息编号为"' + _ids + '"的数据项？').then(function() {
    return delCopy(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>


<style lang="scss">
.el-drawer {
  .el-drawer__body {
    padding: 0 !important;
    border-top: 1px solid #e8e8e8;;
  }

  .el-drawer__header {
    margin-bottom: 5px !important;
  }
}
</style>
